package com.wy.thread_pool.impl;

import java.util.concurrent.BlockingQueue;

/**
 * 一个正式员工要做的工作内容——取任务，执行任务
 **/
public class CoreJob implements Runnable{
    //取任务意味着需要阻塞队列
    private final BlockingQueue<Runnable> workQueue;
    private Runnable firstCommand;
    CoreJob(BlockingQueue<Runnable> workQueue, Runnable firstCommand){
        this.workQueue=workQueue;
        this.firstCommand=firstCommand;
    }
    @Override
    public void run() {
        try {
            //优先先把刚提交的任务做完
            firstCommand.run();
            firstCommand=null;//这里设置为null是为了不影响firstCommand对象被GC的回收

            while(true){//或者while(!Thread.interrupted())
                        //从队列中取任务
                Runnable command = workQueue.take();
                //执行任务
                command.run();
            }
        }catch (InterruptedException ignored) {}//先不管异常状态
    }
}
